package UI;

import action.UserAction;
import entity.User;
import util.MysqlUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.HashMap;

/*
这里是登录界面及部分功能的代码演示
* swing 技术的使用
* 每次添加完新组件以后一定要加上this.getContentPane().add(组件名)!!
* */
public class LoginUI extends JFrame implements MouseListener {
    /*
    * 定义登录界面的所有组件对象包括:标签(账号,密码)，文本框，密码框，按钮(注册,登录)等
    * */
    public static User currentUser=new User();
    //按钮
    JButton jb1 = new JButton("注册");
    JButton jb2 = new JButton("登录");
    //标签
    JLabel userNameLabel,passWordLabel;
    //文本框
    public static JTextField userNameText;
    //密码框
    JPasswordField passWordText;
    //构造类(入口):
    public LoginUI() {

        //加载窗口
        WholeUI wholeUI = new WholeUI(this) {
                    //重写修改标题
                    @Override
                    protected void elseWindow(JFrame jFrame) {
                        jFrame.setTitle("登录界面");
                    }
                };
                //加载按钮
                loadButton();
                //加载标签
                loadLabel();
                //加载文本框和密码框
                loadText();

            }

            private void loadText() {
                userNameText = new JTextField("小王");
                userNameText.setBounds(100, 33, 150, 30);
                this.getContentPane().add(userNameText);
                passWordText = new JPasswordField("123456");
                passWordText.setBounds(100, 74, 150, 30);
                this.getContentPane().add(passWordText);
            }

            private void loadLabel() {
                //用户名标签
                userNameLabel = new JLabel("用户名：");
                userNameLabel.setBounds(52, 33, 54, 15);
                this.getContentPane().add(userNameLabel);


                //密码标签
                passWordLabel = new JLabel("密 码：");       //定义标签对象
                passWordLabel.setBounds(52, 74, 54, 15);
                this.getContentPane().add(passWordLabel);
            }

            private void loadButton() {
                //位置以及大小
                jb1.setBounds(100, 200, 100, 50);
                //给按钮绑定鼠标事件(implements MouseListener的原因)
                jb1.addMouseListener(this);
                //把按钮添加到窗口
                this.getContentPane().add(jb1);
                //显示按钮
                jb1.setVisible(true);
                //位置以及大小
                jb2.setBounds(300, 200, 100, 50);
                //给按钮绑定鼠标事件(implements MouseListener,具体重写方法见下图)
                jb2.addMouseListener(this);
                //把按钮添加到窗口
                this.getContentPane().add(jb2);
                //显示按钮
                jb2.setVisible(true);
            }


            //实现鼠标事件(根据组件对象的不同来实现不同的逻辑)
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getSource() == jb1) {
                    //进入注册界面
                    RegisterUI registerUI = new RegisterUI();
                    this.setVisible(false);

                }
                if (e.getSource() == jb2) {
                    JFrame jFrame1 = new JFrame();
                    jFrame1.setAlwaysOnTop(true);
                    try {
                        User user = new User(userNameText.getText(), passWordText.getText());
                        Integer op= UserAction.login(user);
                        if(op==1){//登录成功
                            currentUser.setUsername(userNameText.getText());
                            //将状态更新到在线状态
                            MysqlUtil.UpdateOnline(user,1);
                            //弹窗
                            JOptionPane.showMessageDialog(jFrame1,"登陆成功");
                            //打开主界面
                            HomeUI homeUI = new HomeUI();
                            //关闭当前页面
                            this.dispose();
                        }else{
                            //弹窗
                            JOptionPane.showMessageDialog(jFrame1, "账号或密码错误");
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
            //检查账户与密码是否正确


            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }

        }



